<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

class JFX_User {
    public $username = 'Guest';
    public $userid = 0;
    public $isAdmin = false;
    public $workspace = 0;
    public $lang = 'en_US';

    public static function getInstance(){
        $CONFIG = JFX::registry('config');
        if(empty($_SESSION[$CONFIG->prefix.'JFX_User'])){
            session_start();
        };
        if(!isset($_SESSION[$CONFIG->prefix.'JFX_User']) || !is_object($_SESSION[$CONFIG->prefix.'JFX_User']) ||
            false === ($_SESSION[$CONFIG->prefix.'JFX_User'] instanceof JFX_User)){
                $USER = new JFX_User;
                $_SESSION[$CONFIG->prefix.'JFX_User'] = $USER;
            }else{
                $USER = $_SESSION[$CONFIG->prefix.'JFX_User'];
            };
        $USER->dateFormat = 'd M Y';
        return $USER;
    }

    public function checkAdminPermission($moduleKey, $label, $workspaceid=''){
        $CONFIG = JFX::registry('config');
        $DB = JFX::registry('db');
        $moduleKey = $DB->escape($moduleKey);
        $label = $DB->escape($label);
        if($DB->countRows($CONFIG->dbprefix.'admin_users_to_permissions', "user_id = '{$this->userid}' AND module_key = 'core' AND permission_key = 'perm_super_admin'")){
            return true;
        }
        $workspaceid = (int) $workspaceid;
        if($workspaceid == 0 || $DB->countRows($CONFIG->dbprefix.'workspaces', "id = '{$workspaceid}'")==0){
            $workspaceid = $this->workspace;
        }
        return (bool) $DB->countRows($CONFIG->dbprefix.'admin_users_to_permissions', "user_id = '{$this->userid}' AND module_key = '{$moduleKey}' AND permission_key = '{$label}' AND workspace_id = '{$workspaceid}'");
    }

    public function isLoggedIn(){
        return (bool) ($this->userid > 0);
    }

    public function isAdmin(){
        return (bool) $this->isAdmin;
    }

    public function login($uData){
        $DB = JFX::registry('db');
        $CONFIG = JFX::registry('config');
        $this->username = $uData['username'];
        $this->userid = $uData['id'];
        // check permission to current workspace
        if($DB->countRows($CONFIG->dbprefix.'admin_users_to_workspaces', "workspace_id = '{$this->workspace}' AND user_id = '{$this->userid}'")==0){
            $wsid = (int) $DB->oneValue($CONFIG->dbprefix.'admin_users_to_workspaces', 'workspace_id', "user_id = '{$this->userid}' AND is_default = 1");
            if($wsid == 0){
                JFX::addError('Could not find a default workspace for your login, this needs to be fixed before you can login');
                return $this->logout();
            }else{
                $this->workspace = $wsid;
            }
        }
        //die('Logged in and assigned to '.$this->workspace);
    }

    public function setAdmin($bool){
        $this->isAdmin = (bool) $bool;
    }

    public function logout(){
        $this->userid = 0;
        $this->username = 'Guest';
    }

    

}